package game.utils
{
	import ash.core.Entity;
	
	import engine.groups.Group;
	import engine.systems.motion.orbit.Orbit;
	import engine.systems.motion.orbit.OrbitSystem;
	import engine.systems.motion.rotateToRadius.RotateToRadius;
	import engine.systems.motion.rotateToRadius.RotateToRadiusSystem;
	import engine.systems.render.render.Render;
	import engine.systems.spatial.spatial.Spatial;
	import engine.systems.spatial.spatialOffset.SpatialOffset;
	import engine.utils.EntityUtils;
	import engine.utils.MathUtils;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.Shape;
	import flash.display.Sprite;

	public class GameUtils
	{
		public static function createLoadingIcon(group:Group, parent:DisplayObjectContainer, name:String = "LoadingIcon"):Entity
		{
			group.addSystem(new RotateToRadiusSystem());
			group.addSystem(new OrbitSystem());
			
			var i:int;
			var sprite:Sprite;
			var shape:Shape;
			var circle:Entity;
			
			var container:Sprite 	= new Sprite();
			container.mouseChildren = false;
			container.mouseEnabled 	= false;
			
			var icon:Entity = new Entity(name);
			icon.add(new Spatial());
			icon.add(new Render(container, parent));
			group.addEntity(icon);
			
			//Back Orbit Container
			sprite 					= new Sprite();
			sprite.mouseChildren 	= false;
			sprite.mouseEnabled 	= false;
			sprite.name 			= "OrbitBack";
			container.addChild(sprite);
			
			shape = new Shape();
			shape.graphics.beginFill(0x333333);
			shape.graphics.drawCircle(0, 0, 55);
			shape.graphics.drawCircle(0, 0, 50);
			
			shape.graphics.beginFill(0xFFFFFF);
			shape.graphics.drawCircle(0, 0, 50);
			shape.graphics.drawCircle(0, 0, 30);
			
			shape.graphics.beginFill(0x333333);
			shape.graphics.drawCircle(0, 0, 30);
			shape.graphics.drawCircle(0, 0, 25);
			shape.graphics.endFill();
			sprite.addChild(shape);
			
			//Colored circles
			var names:Array 	= ["Orange", "Yellow", "Green", "Blue", "Purple", "Red"];
			var colors:Array 	= [0xFF6600, 0xFFFF00, 0x00FF00, 0x0000FF, 0xFF00FF, 0xFF0000];
			
			for(i = 1; i <= colors.length; i++)
			{
				shape = new Shape();
				shape.graphics.beginFill(colors[i - 1]);
				shape.graphics.drawCircle(0, 0, 10);
				shape.graphics.endFill();
				
				circle = new Entity(name + names[i - 1] + "Ball" + i);
				circle.add(new Spatial());
				circle.add(new Render(shape, container));
				circle.add(new SpatialOffset());
				circle.add(new RotateToRadius(40, i, -Math.PI/2));
				group.addEntity(circle);
				
				EntityUtils.addParentChild(icon, circle);
			}
			
			//Front Orbit Container
			sprite 					= new Sprite();
			sprite.mouseChildren 	= false;
			sprite.mouseEnabled 	= false;
			sprite.name 			= "OrbitFront";
			container.addChild(sprite);
			
			var number:int = 12;
			for(i = 1; i <= number; i++)
			{
				shape = new Shape();
				shape.graphics.beginFill(0x333333);
				shape.graphics.drawCircle(0, 0, 10);
				shape.graphics.endFill();
				
				circle = new Entity(name + "Orbit" + i);
				circle.add(new Spatial());
				circle.add(new Render(shape, sprite));
				circle.add(new SpatialOffset());
				circle.add(new Orbit(70, 1, 0, 20, i * MathUtils.PI2/number));
				group.addEntity(circle);
				
				EntityUtils.addParentChild(icon, circle);
			}
			
			return icon;
		}
	}
}